<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MarketController extends Controller
{
     // 分类添加页
    public function getAdd(){

        // 查询所有权限为2,且没有店铺的用户
        $users=DB::table('user')->where('qx',2)->where('mar_status',1)->get();
        // date_diff()($users);
        // var_dump($users);die;

       
        // dd($cates);
        return view('admin.market.add',['users'=>$users]);
    }

    // 分类添加页
    public function postInsert(Request $request)
    {   
        $this->validate($request, [
        'mar_name' => 'required|min:2|max:10',
        'mar_pic' => 'required',
        ],[
            'mar_name.required'=>'商铺名不能为空',
            'mar_name.min'=>'商铺名太短',
            'mar_name.max'=>'商铺名太长',
            'mar_pic.required'=>'商铺LOGO不能为空',
        ]);

        $data = $request->only(['mar_name','use_id']);
        if($request->hasFile('mar_pic')){
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取后缀名
            $su = $request->file('mar_pic')->getClientOriginalExtension();
            // dd($su);jpg png jpeg gif tiff
            $arr = array('jpg','png','jpeg','tiff');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://lamp154.cn/admin";
                </script>';
            }
            //将文件移动到指定目录下
            $request->file('mar_pic')->move('./uploads/', $name.'.'.$su);
            // echo '<img src="/uploads/'.$name.'.jpg" alt="">';
            $data['mar_pic']= $name.'.'.$su;
        }
        // dd($data);
        // var_dump($data['use_id']);die;
        // var_dump($data);die;

        // 执行数据操作
        $res = DB::table('market')->insert($data);
        // var_dump($res);die;
        $ures = DB::table('user')->where('id',$data['use_id'])->update(['mar_status'=>'2']);
        // var_dump($ures);die;
        if($res){

            return redirect('/admin/market/index')->with('success','店铺添加成功');
        }else{
            return back()->with('error','店铺添加失败');
        } 
    }

    // 分类列表
    public function getIndex(Request $request)
    {       
        // dd($request->all());

        $num = $request->input('num',10);
        // $cates = self::CateList();
        if($request->input('keywords')){

         $cates = DB::table('market')->join('user','user.id','=','market.use_id')->select('market.*', 'user.username')->where('mar_name','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            $cates=DB::table('market')->join('user','user.id','=','market.use_id')->select('market.*', 'user.username')->paginate($num);

        // dd($cates);
        }
        
        $data=$request->all();
        return view('admin.market.index',['cates'=>$cates,'request'=>$data]);
    }
   static public function CateList()
    {
         // dd('wwww');
        // $cates = DB::table('cate')->get();
        $cates = DB::select('select *,concat(path,",",id) as paths from cate order by paths');
            // var_dump($cates);die;
        foreach ($cates as $k => $v) {
            // 把字符串变成数组
            $arr = explode(',',$v->path);
            // 获取长度
            $len = count($arr)-1;
            $cates[$k]->name = str_repeat('|----',$len).$v->name;

            # code...
        }
        return $cates;
    }
// 删除分类
     public function postDelete(Request $request)
            {
                // 获取id
                $id = $request->input('id');
               
                // 执行删除
                $res = DB::table('market')->where('id',$id)->delete();
                if($res){
                    // 删除成功
                    echo 2;

                }else{
                    // 删除失败
                    echo 1;
                }
            }
    // 分类修改
   public function getEdit($id) 
    {
        // dd($id);
        $markets = DB::table('market')->where('id',$id)->first();
        // dd($markets);
        
        // echo '2222';
        return view('admin.market.edit',['markets'=>$markets]);
    }

    // 分类数据修改
    public function postUpdate(Request $request)
    {

        $this->validate($request, [
        'mar_name' => 'required|min:2|max:10',
        'mar_pic' => 'required',
        ],[
            'mar_name.required'=>'商铺名不能为空',
            'mar_name.min'=>'商铺名太短',
            'mar_name.max'=>'商铺名太长',
            'mar_pic.required'=>'商铺LOGO不能为空',
        ]);
        // dd($request->all());
        $id = $request->input('id');
        $use_id = $request->input('use_id');
        $data = $request->only(['mar_name','mar_pic']);

        if($request->hasFile('mar_pic')){
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取后缀名
            $su = $request->file('mar_pic')->getClientOriginalExtension();
            // dd($su);jpg png jpeg gif tiff
            $arr = array('jpg','png','jpeg','tiff');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://lamp154.cn/test/add";
                </script>';
            }
            //将文件移动到指定目录下
            $request->file('mar_pic')->move('./uploads/', $name.'.'.$su);
            // echo '<img src="/uploads/'.$name.'.jpg" alt="">';
            $data['mar_pic']= $name.'.'.$su;
        }
       
        // 下面就可以修改
        // dd($data);
        // 执行数据修改操作
        $res = DB::table('market')->where('id',$id)->update($data);

        if($res){
            return redirect('/admin/market/index')->with('success','店铺名称修改成功');
        }else{
            return back()->with('error','店铺名称修改失败');
        } 
    }
}  
